﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FlightClient.NewService;
using System.ServiceModel;

namespace FlightClient
{

    public partial class Form1 : Form
    {
        static NewService.IFlightService flightServiceObj = new NewService.FlightServiceClient();
        City fromCity;
        City toCity;
        CustomProgressBar customProgressBar;
        int algorithm = 0; // 0 all, 1 cheapest, 2 fastest

        DateTime departure;
        int noOfSeats;
        ListOfConnections listOfConnections;
        Connection selected;
        public Form1()
        {

            InitializeComponent();
            IList<City> cities = flightServiceObj.GetCityList();
            IList<City> theSecondList = flightServiceObj.GetCityList();
            customProgressBar = new CustomProgressBar();
            cityFromChoice.DataSource = cities;
            cityFromChoice.DisplayMember = "Name";
            cityToChoice.DataSource = theSecondList;
            cityToChoice.DisplayMember = "Name";


            //foreach(City c in cities)
            //{

            //    cityFromChoice.Items.Add(c);

            //}
        }
        public override string ToString()
        {
            return base.ToString();
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            //DateTimePicker dateTimePicker1 = (DateTimePicker)sender;
            //departure = (DateTime)dateTimePicker1.Value;
        }

        private void Form1_Load(object sender, EventArgs e)
        {


        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {

        }

        private void timeLabel_Click(object sender, EventArgs e)
        {

        }

        private void cityFromLabel_Click(object sender, EventArgs e)
        {

        }

        private void userSettingsGroupBox_Enter(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void cityFromChoice_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (cityFromChoice.SelectedText == "")
            {
                cityFromChoice.Text = "Select departure city";
            }

            else
            {
                cityFromChoice.Text = cityFromChoice.SelectedText;
            }

            ComboBox cmb = (ComboBox)sender;
            fromCity = (City)cmb.SelectedItem;
            airportFromName.Text = fromCity.Country;

        }

        private void airportFromName_TextChanged(object sender, EventArgs e)
        {

        }

        private void cityToChoice_SelectedIndexChanged(object sender2, EventArgs e)
        {

            if (cityToChoice.SelectedText == "")
            {
                cityToChoice.Text = "Select arrival city";
            }

            else
            {
                cityToChoice.Text = cityToChoice.SelectedText;
            }

            ComboBox cmb1 = (ComboBox)sender2;
            toCity = (City)cmb1.SelectedItem;
            airportToName.Text = toCity.Country;
        }

        private void noOfSeatsBox_TextChanged(object sender, EventArgs e)
        {


        }

        private void searchButton_Click(object sender, EventArgs e)
        {
            customProgressBar.Value = 0;
            int count = 0;
            label1.Text = "";
            departure = dateTimePicker1.Value.Date;
            try
            {

                noOfSeats = int.Parse(noOfSeatsBox.Text);

            }
            catch (Exception)
            {

            }

            if (checkDest() == true)
            {
                if (checkDate() == true)
                {
                    if (checkSeats() == true)
                    {

                        AddNewCustomProgressBar();
                        AddNewStatusBar();
                        switch (algorithm)
                        {
                            case 0:
                                listOfConnections = flightServiceObj.getListOfConnections(fromCity, toCity, noOfSeats, departure);

                                listView1.Items.Clear();

                                foreach (Connection c in listOfConnections.ConnectionList)
                                {
                                    ListViewItem lvl = new ListViewItem(c.DepartureTime.ToString());
                                    lvl.SubItems.Add(c.ArrivalTime.ToString());
                                    lvl.SubItems.Add(c.TotalPrice.ToString());
                                    lvl.SubItems.Add(c.NumberOfFlightChange.ToString());
                                    lvl.Tag = c;
                                    listView1.Items.Add(lvl);
                                    //.SubItems.Add(c.DepartureTime);
                                    count++;

                                    // , , , );

                                }
                                break;

                            case 1:



                                listView1.Items.Clear();
                                try
                                {
                                    Connection c2 = flightServiceObj.getCheapest(fromCity, toCity, noOfSeats, departure);
                                    ListViewItem lvl2 = new ListViewItem(c2.DepartureTime.ToString());
                                    lvl2.SubItems.Add(c2.ArrivalTime.ToString());
                                    lvl2.SubItems.Add(c2.TotalPrice.ToString());
                                    lvl2.SubItems.Add(c2.NumberOfFlightChange.ToString());
                                    lvl2.Tag = c2;
                                    listView1.Items.Add(lvl2);
                                    count++;
                                }
                                catch (Exception)
                                {
                                    MessageBox.Show("Sorry, we couldn't find any flight that would meet your preferences. Please adjust time scope and number of seats",
               "Important Note",
               MessageBoxButtons.OK,
               MessageBoxIcon.Exclamation,
               MessageBoxDefaultButton.Button1);
                                }
                                break;
                            case 2:

                                listView1.Items.Clear();
                                try
                                {
                                    Connection c3 = flightServiceObj.getFastest(fromCity, toCity, noOfSeats, departure);
                                    ListViewItem lvl3 = new ListViewItem(c3.DepartureTime.ToString());
                                    lvl3.SubItems.Add(c3.ArrivalTime.ToString());
                                    lvl3.SubItems.Add(c3.TotalPrice.ToString());
                                    lvl3.SubItems.Add(c3.NumberOfFlightChange.ToString());
                                    lvl3.Tag = c3;
                                    listView1.Items.Add(lvl3);
                                    count++;
                                }
                                catch (Exception)
                                {
                                    MessageBox.Show("Sorry, we couldn't find any flight that would meet your preferences. Please adjust time scope and number of seats",
               "Important Note",
               MessageBoxButtons.OK,
               MessageBoxIcon.Exclamation,
               MessageBoxDefaultButton.Button1);
                                }
                                break;
                        }
                    }
                    else
                    {
                        MessageBox.Show("Please choose a number of seats.",
        "Important Note",
        MessageBoxButtons.OK,
        MessageBoxIcon.Exclamation,
        MessageBoxDefaultButton.Button1);
                    }
                }
                else
                {
                    MessageBox.Show("Date error.",
        "Important Note",
        MessageBoxButtons.OK,
        MessageBoxIcon.Exclamation,
        MessageBoxDefaultButton.Button1);
                }
            }
            //MessageBox.Show(listOfConnections.ConnectionList[0].DestinationCity.Name, "My Application", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
            else
            {
                MessageBox.Show("Please select two different cities for arrival and departure.",
        "Important Note",
        MessageBoxButtons.OK,
        MessageBoxIcon.Exclamation,
        MessageBoxDefaultButton.Button1);
            }

           label1.Text = count + " connections was found";
        }

        private System.Windows.Forms.ProgressBar AddNewCustomProgressBar()
        {

            this.Controls.Add(customProgressBar);
            customProgressBar.Location = new System.Drawing.Point(24, 415);
            customProgressBar.Width = 284;
            customProgressBar.Height = 25;
            customProgressBar.Style = ProgressBarStyle.Blocks;
            customProgressBar.Name = "customerProgressBar";

            for (int i = 0; i < 101; i++)
            {
                customProgressBar.Value = i;
                customProgressBar.DisplayStyle = ProgressBarDisplayText.Percentage;



                System.Threading.Thread.Sleep(3);
                if (i > 0 || i < 20)
                {
                    customProgressBar.Tag = "Initializing";
                    customProgressBar.Show();
                }
                if (i > 40)
                {
                    customProgressBar.CustomText = "Connecting to database";
                }

                if (i > 60)
                {
                    customProgressBar.CustomText = "Results are retrieved";
                }

                if (i > 80)
                {
                    customProgressBar.CustomText = "Search is completed";
                }

                System.Threading.Thread.EndCriticalRegion();
            }
            return customProgressBar;
        }

        private System.Windows.Forms.StatusBar AddNewStatusBar()
        {

            System.Windows.Forms.StatusBar sb = new System.Windows.Forms.StatusBar();
            this.Controls.Add(sb);
            sb.Name = "statusBar";

            if (sb.Created)
            {
                sb.Visible = true;
                sb.Text = "Search is initiated";
                System.Threading.Thread.Sleep(400);
                sb.Visible = false;
            }

            if (sb.IsDisposed)
            {
                sb = null;
            }

            else
            {
                sb.Visible = true;
                sb.Text = "Search is retrieving results from database";
                System.Threading.Thread.Sleep(150);
                sb.Visible = false;


            }

            return sb;
        }




        private void proceedButton_Click(object sender, EventArgs e)
        {
            try
            {
                Connection n = (Connection)listView1.SelectedItems[0].Tag;

                Form2 popup = new Form2(n, int.Parse(noOfSeatsBox.Text));
                DialogResult dialogresult = popup.ShowDialog();
            }
            catch (Exception)
            {

                MessageBox.Show("Please select connnection",
        "Important Note",
        MessageBoxButtons.OK,
        MessageBoxIcon.Exclamation,
        MessageBoxDefaultButton.Button1);

            }


        }

        private void dateTimePicker1_ValueChanged_1(object sender, EventArgs e)
        {

            //connectionInfoBox.Text = departure.ToString();
        }

        private void connectionInfoBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void airportToName_TextChanged(object sender, EventArgs e)
        {

        }


        private void listView2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (listView1.SelectedItems.Count > 0)
            {
                Connection conn = (Connection)listView1.SelectedItems[0].Tag;
                listView3.Items.Clear();

                //MessageBox.Show(cn.DepartureCity.Name, "My Application",
                //  MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);

                foreach (Flight f in conn.Flights)
                {
                    ListViewItem lvl = new ListViewItem(f.FromName.ToString());
                    lvl.SubItems.Add(f.ToName.ToString());
                    lvl.SubItems.Add(f.DepartureTime.ToString());
                    lvl.SubItems.Add(f.ArrivalTime.ToString());
                    lvl.SubItems.Add(f.PriceOfTheOneSeat.ToString());
                    listView3.Items.Add(lvl);

                }
            }

        }
        private Boolean checkDest()
        {
            Boolean ok = true;
            if (fromCity.Name.Equals(toCity.Name))
                ok = false;
            return ok;
        }
        private Boolean checkDate()
        {
            Boolean ok = true;
            DateTime a = this.dateTimePicker1.Value.Date;
            DateTime b = this.dateTimePicker2.Value.Date;
            int result = DateTime.Compare(a, b);
            if (result > 0)
                ok = false;
            return ok;
        }
        private Boolean checkSeats()
        {

            int n;

            bool isNumeric = int.TryParse(noOfSeatsBox.Text, out n);
            return isNumeric;
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            algorithm = 0;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            algorithm = 1;
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            algorithm = 2;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }


    }
}
